import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
import { UploadModule } from './upload/upload.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { IdCardModule } from './id-card/id-card.module';
import { JobModule } from './job/job.module';
import { TagModule } from './tag/tag.module';
import { CityModule } from './city/city.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { getConfig, getProConfig } from './envConfig/yamlConfig';
// import { JwtModule } from '@nestjs/jwt';
import { AuthModule } from './auth/auth.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { EmailModule } from './email/email.module';
import { RedisModule } from './redis/redis.module';
import { User } from './user/entities/user.entity';
import { Job } from './job/entities/job.entity';
import { IdCard } from './id-card/entities/id-card.entity';
import { Tag } from './tag/entities/tag.entity';
import { City } from './city/entities/city.entity';
import { OssModule } from './oss/oss.module';
import { MinioModule } from './minio/minio.module';
import { SocketModule } from './socket/socket.module';
import { WsGuard } from './utils/guard/WsGuard';
import { APP_GUARD } from '@nestjs/core';

@Module({
  imports: [
    UserModule,
    UploadModule,
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'public'), // 设置静态文件夹路径
      serveRoot: '/static',
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      load: [getConfig],
      // load: [getProConfig],
    }),

    TypeOrmModule.forRootAsync({
      useFactory(ConfigService: ConfigService) {
        const dbConfig = ConfigService.get('database');
        return {
          type: 'mysql',
          host: dbConfig.host,
          port: dbConfig.port,
          username: dbConfig.username,
          password: dbConfig.password,
          database: dbConfig.database,
          logging: true,
          synchronize: false,
          // entities: ['./**/*.entity{.ts,.js}'],
          poolSize: 10,
          connectorPackage: 'mysql2',
          extra: {
            authPlugin: 'sha256_password',
          },
          entities: [User, Job, IdCard, Tag, City],
          // synchronize: true,
          // autoLoadEntities: true,
          // retryDelay: 500,
          // retryAttempts: 10,
        };
      },
      inject: [ConfigService],
    }),
    IdCardModule,
    JobModule,
    TagModule,
    CityModule,
    AuthModule,
    EmailModule,
    RedisModule,
    OssModule,
    MinioModule,
    SocketModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'validation_options',
      useFactory() {
        return {
          aaa: 111,
          bbb: 222,
        };
      },
    },
    {
      provide: APP_GUARD,
      useClass: WsGuard,
    },
  ],
})
export class AppModule {
  constructor() {
    console.log('app module');
  }
}
